package com.example.demo.util;

import java.io.UnsupportedEncodingException;

/**
 * @Classname GBKDemo
 * @Description TODO
 * @Date 2019/9/26 12:57
 * @Created by yby
 */
public class GBKDemo {
    public static void main(String[] args) throws Exception {
        String a = "成功";

        byte[] b = a.getBytes("gbk");
        System.out.println(bytesToHexFun1(b));


    }

    //将byte数组转成16进制字符串
    public static String bytesToHexFun1(byte[] bytes) {
        char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
                '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        // 一个byte为8位，可用两个十六进制位标识
        char[] buf = new char[bytes.length * 2];
        int a = 0;
        int index = 0;
        for (byte b : bytes) { // 使用除与取余进行转换
            if (b < 0) {
                a = 256 + b;
            } else {
                a = b;
            }
            buf[index++] = HEX_CHAR[a / 16];
            buf[index++] = HEX_CHAR[a % 16];
        }
        return new String(buf);
    }
}
